package org.dingxf.util;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentArray {

    private static String[] student = {"小明", "小红", "小黄"};
    private static int code = 3;


    public static void main(String[] args) {

        while (true) {
            //执行开始方法
            start();
            int nextInt = 0;
            try {
                nextInt = new Scanner(System.in).nextInt();
            } catch (Exception e) {
                System.out.println("请输入正确指令");
                System.out.println();

                //返回菜单
                ret();
            }
            switch (nextInt) {
                case 1:
                    queryList();
                    break;
                case 2:
                    queryById();
                    break;
                case 3:
                    insert();
                    break;
                case 4:
                    del();
                    break;
                case 5:
                    update();
                case 9:
                    multiplication();
                    break;
            }

            //结束操作
            if (nextInt == 0) {
                System.out.println("感谢使用！");
                break;
            }
        }

    }

    //菜单
    public static void start() {
        System.out.println("-------学生姓名管理系统---------");
        System.out.println("1.查看所有信息          ");
        System.out.println("2.根据下标查找          ");
        System.out.println("3.添加姓名          ");
        System.out.println("4.删除姓名          ");
        System.out.println("5.修改姓名          ");
        System.out.println("9.九九乘法表          ");
        System.out.println("0.退出系统          ");
        System.out.println("-------------------------------");
        System.out.println();
        System.out.println("请输入您的指令：");
    }

    public static void ret() {
        System.out.println();
        Scanner sc = new Scanner(System.in);
        System.out.println("输入任意字母或数字建+回车返回主菜单");
        String next = sc.next();
    }

    //查询操作
    public static void queryList() {
        System.out.println("查询结果----------");
        System.out.println();
        for (int i = 0; i < code; i++) {
            System.out.print(student[i]+", ");
        }
        System.out.println();

        System.out.println("查询结果----------");
        //返回菜单操作
        ret();
    }

    //筛选
    public static void queryById() {
        System.out.println("请输入您要查找的下标：");
        Scanner scanner = new Scanner(System.in);
        int next = scanner.nextInt();

        if (next<code){
            String s = student[next];
            System.out.println();
            System.out.println("您查找的信息姓名为：" + s);
            ret();
        }else {
            System.out.println("小标越界，为您返回主菜单...");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ignored) { }
        }


/*        try {
            String s = student[next];
            System.out.println();
            System.out.println("您查找的信息姓名为：" + s);
            ret();
        } catch (Exception e) {
            System.out.println("小标越界，为您返回主菜单...");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ignored) { }
        }*/
    }

    //添加
    public static void insert() {

        ArrayList list = new ArrayList();

        System.out.println("请输入您要添加的姓名：");
        String name = new Scanner(System.in).next();

        //判断数组是否以满
        if (code==student.length){
            String[] stu = new String[student.length*2];
            for (int i = 0; i < student.length; i++) {
                stu[i] = student[i];
            }
            stu[code] = name;
            student = stu;
        }else {
            student[code] = name;
        }
        code++;
        System.out.println("添加成功！");
        ret();
    }

    //删除
    public static void del() {
        System.out.println("请输入您要删除的姓名：");
        String name = new Scanner(System.in).next();

        int flag = 9999;

        for (int i = 0; i < student.length; i++) {
            if (student[i].equals(name)){
                flag = i;
                break;
            }
        }
        if (flag==9999) System.out.println("为查询到此用户!");

        //如果此数据在最后一位
        if ((flag + 1) == code) {
            student[flag] = null;
        } else {
            if (code - flag >= 0) System.arraycopy(student, flag + 1, student, flag, code - flag);

           /* for (int i = flag; i < code; i++) {
                student[i] = student[i + 1];
            }*/
        }
        code--;
        System.out.println("删除成功！");
        ret();
    }

    //修改
    public static void update() {
        System.out.println("请输入您要修改的姓名：");
        String next = new Scanner(System.in).next();
        boolean flag = false;
        for (int i = 0; i < student.length; i++) {
            if (student[i].equals(next)) {
                System.out.println("请输入新姓名：");
                String name = new Scanner(System.in).next();
                student[i] = name;
                System.out.println("修改成功！");
                flag = false;
                break;
            }
            flag = true;
        }
        if (flag) System.out.println("未匹配到对应信息！");
        ret();
    }

    //九九乘法表
    public static void multiplication(){
        System.out.println("九九乘法表打印开始-------");
        System.out.println();
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"X"+i+"="+i*j+" ");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println("九九乘法表打印结束-------");
        ret();
    }

}
